package com.youlian.cloud.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.VersionResourceResolver;

/**
 * @author liuhx
 * @create 2019/08/01 17:18
 * @email liuhx@elab-plus.com
 **/
public class MvcCommonConfiguration implements WebMvcConfigurer {

    @Value("${swagger2.enable}")
    private boolean enableSwagger;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (enableSwagger) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/static/");
            registry.addResourceHandler("/swagger/**").addResourceLocations("/swagger/")
                    .resourceChain(true).addResolver(new VersionResourceResolver().addFixedVersionStrategy("1.10", "/**/*.js").addContentVersionStrategy("/**"));

            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/")
                    .resourceChain(true).addResolver(new VersionResourceResolver().addFixedVersionStrategy("1.10", "/**/*.js").addContentVersionStrategy("/**"));

            registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/")
                    .resourceChain(true).addResolver(new VersionResourceResolver().addFixedVersionStrategy("1.10", "/**/*.js").addContentVersionStrategy("/**"));

            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/")
                    .resourceChain(true).addResolver(new VersionResourceResolver().addFixedVersionStrategy("1.10", "/**/*.js").addContentVersionStrategy("/**"));
        }
    }
}
